﻿using Datory;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SSCMS.Dto;
using SSCMS.Repositories;
using SSCMS.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WF.BeOnDuty.BeOnDuty;
using WF.BeOnDuty.Models;

namespace WF.BeOnDuty.Core
{
    public class DutyInfoRepository : IDutyInfoRepository
    {
        private readonly IPathManager _pathManager;
        private readonly ISiteRepository _siteRepository;
        private readonly Repository<Models.DutyInfo> _repository;
        public DutyInfoRepository(IPathManager pathManager, ISettingsManager settingsManager, ISiteRepository siteRepository)
        {
            _pathManager = pathManager;
            _siteRepository = siteRepository;
            _repository = new Repository<Models.DutyInfo>(new Database(settingsManager.DatabaseType, settingsManager.DatabaseConnectionString), settingsManager.Redis);
        }

        public async Task<bool> DeleteAsync(int siteId, int id)
        {
            return await _repository.DeleteAsync(id);
        }
        public async Task<Models.DutyInfo> GetDutyDateAsync(int siteId, DateTime dutyDate) 
        {
            return await _repository.GetAsync(Q
                .Where(nameof(Models.DutyInfo.SiteId), siteId)
                .WhereDate(nameof(Models.DutyInfo.DutyDate),"=", dutyDate.ToString("yyyy-MM-dd")));
        }

        public async Task<List<DutyInfo>> GetAllAsync(int siteId)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(Models.DutyInfo.SiteId), siteId)
                .OrderByDesc(nameof(Models.DutyInfo.CreatedDate))
            );
        }

        public async Task<DutyInfo> GetAsync(int siteId, int id)
        {
            return await _repository.GetAsync(id);
        }

        public async Task<int> GetCountAsync(int siteId)
        {
            return await _repository.CountAsync(Q.Where(nameof(Models.DutyInfo.SiteId), siteId));
        }

        public async Task<List<DutyInfo>> GetPageAsync(int siteId, int page, int perPage = 15)
        {
            return await _repository.GetAllAsync(Q
                .Where(nameof(Models.DutyInfo.SiteId), siteId)
                .ForPage(page, perPage)
                .OrderByDesc(nameof(Models.DutyInfo.DutyDate))
            );
        }
        /// <summary>
        /// 含今天往后的数据 
        /// </summary>
        /// <param name="siteId">站点</param>
        /// <param name="count">top数</param>
        /// <returns></returns>
        public async Task<List<Models.DutyInfo>> GetToDayCountAsync(int siteId, int count)
        {
            return await _repository.GetAllAsync(Q
                    .Where(nameof(Models.DutyInfo.SiteId), siteId)
                    .WhereDate(nameof(Models.DutyInfo.DutyDate),">=",DateTime.Now.AddDays(-1).Date)
                    .Take(count)
                    .OrderBy(nameof(Models.DutyInfo.DutyDate))
             );
        }
        public async Task<int> InsertAsync(DutyInfo model)
        {
            return await _repository.InsertAsync(model);
        }

        public async Task<bool> UpdateAsync(DutyInfo model)
        {
            return await _repository.UpdateAsync(model);
        }
    }
}
